package Engine.RMI
{
    import Framework.Serialize.*;
    import Framework.Util.*;
    import flash.events.*;
    import flash.utils.*;

    public class RMIObjectBind extends Object
    {
        private var _session:Session;
        private var _rmiObject:RMIObject;
        private var _buffer:SerializeStream;
        private var _outgoingStatus:int;
        private var _messageId:int;
        private var _timer:Timer;
        private var _createTimer:Number;
        public static const EOutGoingWaitToSend:int = 0;
        public static const EOutGoingTimeOut:int = 1;
        public static const EOutGoingComplate:int = 2;

        public function RMIObjectBind()
        {
            this._outgoingStatus = EOutGoingWaitToSend;
            this._createTimer = getTimer();
        }

        public function initTimer(time:int) : void
        {
            if (time >= 0)
            {
                this._timer = new Timer(time, 1);
                this._timer.addEventListener(TimerEvent.TIMER_COMPLETE, this.onComplete);
                this._timer.start();
            }
        }

        public function resertTimer() : void
        {
            if (this._timer)
            {
                this._timer.reset();
                this._timer = null;
            }
        }

        private function onComplete(event:TimerEvent) : void
        {
            var exception:TimeoutException = null;
            this._session.removeBackObject(this.messageId);
            if (this._timer != null)
            {
                this._timer.reset();
            }
            this._outgoingStatus = EOutGoingTimeOut;
            if (this._rmiObject != null)
            {
				exception = new TimeoutException();
                if (this._session.communicator.prepareCommandHandler)
                {
                    this._rmiObject.usedTimer = getTimer() - this._createTimer;
                    this._session.communicator.prepareCommandHandler.prepareBackException(this._rmiObject, exception);
                }
                this._rmiObject.cdeException(exception);
            }
        }

        public function __back(context:Context, stream:SerializeStream) : void
        {
            var exception:Exception = null;
            this._outgoingStatus = EOutGoingComplate;
            if (context._dispachStatus.equals(ERMIDispatchStatus.DispatchOK))
            {
                if (this._session.communicator.prepareCommandHandler)
                {
                    this._rmiObject.usedTimer = getTimer() - this._createTimer;
                    this._session.communicator.prepareCommandHandler.prepareBackCommand(this._rmiObject);
                }
                this.rmiObject.__response(stream);
            }
            else if (context._dispachStatus.equals(ERMIDispatchStatus.DispatchException))
            {
                exception = new Exception();
                stream.readException(exception);
                if (this._session.communicator.prepareCommandHandler)
                {
                    this._rmiObject.usedTimer = getTimer() - this._createTimer;
                    this._session.communicator.prepareCommandHandler.prepareBackException(this._rmiObject, exception);
                }
                this._rmiObject.cdeException(exception);
            }
        }

        public function __response(dispatchStatus:ERMIDispatchStatus, stream:SerializeStream) : void
        {
            if (this._messageId == 0)
            {
                
            }
            var srmiReturn:* = new SRMIReturn();
            srmiReturn.messageId = this._messageId;
            srmiReturn.dispatchStatus = dispatchStatus;
            var context:* = new Context();
            new Context()._session = this.session;
            context._connection = this.session.connection;
            Outgoing.dispatch(context, srmiReturn, stream);
        }

        public function __exception(exception:Exception) : void
        {
            if (this._messageId == 0)
            {
                
            }
            var srmiReturn:* = new SRMIReturn();
            srmiReturn.messageId = this._messageId;
            srmiReturn.dispatchStatus = ERMIDispatchStatus.DispatchException;
            var context:* = new Context();
            context._session = this.session;
            context._connection = this.session.connection;
            var stream:* = new SerializeStream();
            new SerializeStream().writeException(exception);
            Outgoing.dispatch(context, srmiReturn, stream);
        }

        public function set session(session:Session) : void
        {
            this._session = session;
        }

        public function get session() : Session
        {
            return this._session;
        }

        public function set rmiObject(rmiObject:RMIObject) : void
        {
            this._rmiObject = rmiObject;
        }

        public function get rmiObject() : RMIObject
        {
            return this._rmiObject;
        }

        public function set buffer(buffer:SerializeStream) : void
        {
            this._buffer = buffer;
        }

        public function get buffer() : SerializeStream
        {
            return this._buffer;
        }

        public function set outgoingStatus(outgoingStatus:int) : void
        {
            this._outgoingStatus = outgoingStatus;
        }

        public function get outgoingStatus() : int
        {
            return this._outgoingStatus;
        }

        public function set messageId(messageId:int) : void
        {
            this._messageId = messageId;
        }

        public function get messageId() : int
        {
            return this._messageId;
        }

    }
}
